package com.dayuanit.dy17.atm.task;

import com.dayuanit.dy17.atm.entity.TransferOrder;
import com.dayuanit.dy17.atm.enums.TransferOrderStatusEnum;
import com.dayuanit.dy17.atm.service.ICardService;
import com.dayuanit.dy17.atm.service.ITransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class TransferRollbackTask extends BaseTask {

    @Autowired
    private ITransferService transferService;

    @Scheduled(cron = "0/20 * * * * ?")
    @Override
    public void doTask() {
        System.out.println("----------TransferRollbackTask------------");
        processTranasferRollback();
    }

    private void processTranasferRollback() {
        List<TransferOrder> transferOrders = transferService.loadDelayTransferOrder(TransferOrderStatusEnum.rollback_transfer);
        for (TransferOrder order : transferOrders) {
            try {
                transferService.transferRollback(order);
            } catch (Exception e) {
                e.printStackTrace();
                // 人工介入
                transferService.changeOrderStatus(order, TransferOrderStatusEnum.human_transfer);
            }
        }
    }
}
